package com.mindskip.xzs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.domain.Department;
import com.mindskip.xzs.domain.DepartmentUser;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.UserEventLog;
import com.mindskip.xzs.event.CalculateExamPaperAnswerCompleteEvent;
import com.mindskip.xzs.event.UserEvent;
import com.mindskip.xzs.exception.BusinessException;
import com.mindskip.xzs.repository.DepartmentMapper;
import com.mindskip.xzs.repository.DepartmentUserMapper;
import com.mindskip.xzs.repository.NewUserMapper;
import com.mindskip.xzs.service.DepartmentUserService;
import com.mindskip.xzs.utility.ExamUtil;
import com.mindskip.xzs.viewmodel.admin.dept.AddSidelineReq;
import com.mindskip.xzs.viewmodel.admin.dept.SidelineRes;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class DepartmentUserServiceImpl extends ServiceImpl<DepartmentUserMapper, DepartmentUser> implements DepartmentUserService {

    @Resource
    private NewUserMapper newUserMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private ApplicationEventPublisher eventPublisher;

    @Override
    public PageInfo<SidelineRes> pageSideline(Integer userId, Long deptId,Integer pageNum,Integer pageSize) {
        return PageHelper.startPage(pageNum == null ? 1 : pageNum, pageSize == null ? 1 : pageSize, "du.id desc").doSelectPageInfo(() ->
                baseMapper.pageSideline(userId, deptId)
        );
    }

    @Override
    public Boolean addSideline(AddSidelineReq req) {
        User user = newUserMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getId, req.getUserId()));
        if (user == null) {
            throw new BusinessException("员工不存在");
        }
        DepartmentUser departmentUser = new DepartmentUser()
                .setSideline(true)
                .setUserQywxId(user.getQywxId())
                .setDeptId(req.getDeptId());
        return save(departmentUser);
    }

    @Override
    public Boolean delSideline(Integer id, User user) {
        DepartmentUser departmentUser = getById(id);
        User user1 = newUserMapper.selectOne(
                new LambdaQueryWrapper<User>()
                        .eq(User::getQywxId, departmentUser.getUserQywxId())
        );
        Department department = departmentMapper.selectById(departmentUser.getDeptId());
        UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date());
        String content = String.format("%s删除【%s】在【%s】的兼职记录", user.getUserName(), user1.getUserName(), department.getName());
        userEventLog.setContent(content);
        eventPublisher.publishEvent(new UserEvent(userEventLog));
        return removeById(id);
    }
}
